package com.yc.utilslib.reader.card;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * 作者：TanTun
 * 时间：2017/2/28
 * 邮箱：32965926@qq.com
 * 描述：扩展信息实体类
 */

public class ExtendedInfo implements Parcelable {
    private String mEmployeeStrId;
    private String mEmployeeName;
    private String mCardType;

    /**
     * 扩展信息构造函数
     *
     * @param data 扩展信息数据
     */
    private ExtendedInfo(byte[] data) {
        if (data.length <= 0) {
            return;
        }

        try {
            int offset = 0;
            int length = data[offset] & 0x0FF;
            offset = offset + 1;
            if (offset + length < data.length) {
                mEmployeeStrId = new String(data, offset, length, "gb2312");
            } else {
                mEmployeeStrId = "";
            }

            offset = offset + length;
            if (offset >= data.length) return;
            length = data[offset] & 0x0FF;
            offset = offset + 1;
            if (offset + length < data.length) {
                mEmployeeName = new String(data, offset, length, "gb2312");
            } else {
                mEmployeeName = "";
            }

            offset = offset + length;
            if (offset >= data.length) return;
            length = data[offset] & 0x0FF;
            offset = offset + 1;
            if (offset + length < data.length) {
                mCardType = new String(data, offset, length, "gb2312");
            } else {
                mCardType = "";
            }
        } catch (java.io.UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }

    protected ExtendedInfo(Parcel in) {
        mEmployeeStrId = in.readString();
        mEmployeeName = in.readString();
        mCardType = in.readString();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mEmployeeStrId);
        dest.writeString(mEmployeeName);
        dest.writeString(mCardType);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<ExtendedInfo> CREATOR = new Creator<ExtendedInfo>() {
        @Override
        public ExtendedInfo createFromParcel(Parcel in) {
            return new ExtendedInfo(in);
        }

        @Override
        public ExtendedInfo[] newArray(int size) {
            return new ExtendedInfo[size];
        }
    };

    /**
     * 根据传入的数据构造一个新的实例
     *
     * @param data 扩展信息数据
     * @return 扩展信息
     */
    static ExtendedInfo getInstance(byte[] data) {
        return new ExtendedInfo(data);
    }

    /**
     * @return 人员编号
     */
    public String getEmployeeStrId() {
        return mEmployeeStrId;
    }

    /**
     * @return 用户名称
     */
    public String getEmployeeName() {
        return mEmployeeName;
    }

    /**
     * @return 用户卡类
     */
    public String getCardType() {
        return mCardType;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(50);
        sb.append("人员编号：").append(mEmployeeStrId)
                .append(System.getProperty("line.separator"));
        sb.append("用户名称：").append(mEmployeeName)
                .append(System.getProperty("line.separator"));
        sb.append("用户卡类：").append(mCardType);
        return sb.toString();
    }
}
